﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNidePacking : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            tbl_siconide_packing model = new tbl_siconide_packing();
            model.PREVIOUS_PROCESS = ddlPREVIOUS_PROCESS.SelectedValue.Trim();
            model.PALLA_MILLJET_RUN_NUMBER = txtPALLA_MILLJET_RUN_NUMBER.Text.Trim();
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.Drum_Number = txtDrum_Number.Text.Trim();
            model.Gross_Weight = Convert.ToDecimal(txtGross_Weight.Text.Trim());
            model.Net_Weight = Convert.ToDecimal(txtNet_Weight.Text.Trim());
            model.SicoNide_Purity_Grade = txtSicoNide_Purity_Grade.Text.Trim();
            model.SicoNide_SSA = Convert.ToDecimal(txtSicoNide_SSA.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_siconide_packing bll = new BLL_tbl_siconide_packing();
            try
            {


                
                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_siconide_packing model = new tbl_siconide_packing();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.PREVIOUS_PROCESS = ddlPREVIOUS_PROCESS.SelectedValue.Trim();
            model.PALLA_MILLJET_RUN_NUMBER = txtPALLA_MILLJET_RUN_NUMBER.Text.Trim();
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.Drum_Number = txtDrum_Number.Text.Trim();
            model.Gross_Weight = Convert.ToDecimal(txtGross_Weight.Text.Trim());
            model.Net_Weight = Convert.ToDecimal(txtNet_Weight.Text.Trim());
            model.SicoNide_Purity_Grade = txtSicoNide_Purity_Grade.Text.Trim();
            model.SicoNide_SSA = Convert.ToDecimal(txtSicoNide_SSA.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_siconide_packing bll = new BLL_tbl_siconide_packing();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconide_packing model = new tbl_siconide_packing();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconide_packing bll = new BLL_tbl_siconide_packing();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_siconide_packing bll = new BLL_tbl_siconide_packing();
                List<tbl_siconide_packing> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconide_packing tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    ddlPREVIOUS_PROCESS.SelectedValue = tbl.PREVIOUS_PROCESS;
                    txtPALLA_MILLJET_RUN_NUMBER.Text = tbl.PALLA_MILLJET_RUN_NUMBER;
                    txtCharge_Number.Text = tbl.Charge_Number.ToString();
                    txtDrum_Number.Text = tbl.Drum_Number;
                    txtGross_Weight.Text = tbl.Gross_Weight.ToString();
                    txtNet_Weight.Text = tbl.Net_Weight.ToString();
                    txtSicoNide_Purity_Grade.Text = tbl.SicoNide_Purity_Grade;
                    txtSicoNide_SSA.Text = tbl.SicoNide_SSA.ToString();
                    txtRemark.Text = tbl.Remark;
                    

                }
            }
        }
    }


    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (ddlPREVIOUS_PROCESS.SelectedIndex == 0) return "Please select Previous Process";
            if (txtPALLA_MILLJET_RUN_NUMBER.Text == "") return "Please enter Palla/MillJet Run #";
            if (txtCharge_Number.Text == "") return "Please enter Charge Number";
            if (txtDrum_Number.Text == "") return "Please enter Drum Number";
            if (txtGross_Weight.Text == "") return "Please enter Gross Weight";


            if (txtNet_Weight.Text == "") return "Please enter Net Weight";
            if (txtSicoNide_Purity_Grade.Text == "") return "Please enter SicoNide Purity Grade";
            if (txtSicoNide_SSA.Text == "") return "Please enter SicoNide-SSA";


            if (txtRemark.Text == "") return "Please enter Remark";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion
                
}